
#ifndef __MSG_MANAGER_H__
#define __MSG_MANAGER_H__

class cMsgManager : public iMsgManager
{
public:
	static cMsgManager* it();

public:
	cMsgManager();
	virtual ~cMsgManager();

	virtual tBool SendMsg(sMsg& msg);
	virtual tBool SendMsg(tMsgTargetID targetID, sMsg& msg);
	virtual tBool PostMsg(sMsg& msg, tFloat time);
	virtual tBool PostMsg(tMsgTargetID targetID, sMsg& msg, tFloat time);
	virtual tSize CancelPostMsgTo(tMsgTargetID targetID);

	virtual tBool RegisterTarget(iMsgTargetBase* target);
	virtual tBool UnregisterTarget(iMsgTargetBase* target);
	virtual void Update(tFloat dt);

private:
	iMsgTargetBase* FindTarget(tMsgTargetID id);

private:

	typedef tMap<tMsgTargetID, iMsgTargetBase*> tTargetMap;
	tTargetMap m_targetMap;

	struct sPostMsgInfo
	{
		sPostMsgInfo() : msg(NULL), 
			time(0.f){}
		sMsg* msg;
		float time;
	};

	typedef tList<sPostMsgInfo> tPostMsgList;

	struct sPostMsgUpdateInfo
	{
		sPostMsgUpdateInfo()
		{
			target = NULL;
		}
		INOUT sPostMsgInfo* target;
		tPostMsgList::iterator i_target;
	};

	typedef tVector<sPostMsgUpdateInfo> tPostMsgUpdateInfoArray;
	tPostMsgList m_postMsgList;

	typedef tSet<tMsgTargetID> tMsgTargetIDSet;
	tMsgTargetIDSet m_cancelMsgTargetIDSet;
};

#endif